﻿using System;
using System.Collections.Generic;
using System.Text;
using Fadada.FASC.SDK.DotNetCore.Interfaces;
using System.Threading.Tasks;
using Fadada.FASC.SDK.DotNetCore.Model;
using Fadada.FASC.SDK.DotNetCore.Utils;
using Newtonsoft.Json;

namespace Fadada.FASC.SDK.DotNetCore.Services
{
    public class GetAuthTokenService : IGetAuthTokenService
    {
        public async Task<ResponseBodyInfo<RespTokenInfo>> GetAuthTokenAsync(BaseInputInfo inputInfo)
        {
            inputInfo.ReqEndPoint = "/service/get-access-token";
            string strTimeStamp = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000).ToString();//毫秒值
            string strGUID = Guid.NewGuid().ToString("N");
            Dictionary<string, string> dicInput = new Dictionary<string, string>();
            dicInput.Add("X-FASC-App-Id", inputInfo.APP_ID);
            dicInput.Add("X-FASC-Sign-Type", "HMAC-SHA256");
            dicInput.Add("X-FASC-Timestamp", strTimeStamp);
            dicInput.Add("X-FASC-Nonce", strGUID);
            dicInput.Add("X-FASC-Grant-Type", "client_credential");
            //string strSortDicInput = SortParameters.DictonarySort(dicInput);
            //string strSignText = DigestUtils.Sha256Hex(strSortDicInput);
            //byte[] byteSecretSigning = HMACSHA256.Compute(strTimeStamp, Encoding.UTF8.GetBytes(inputInfo.APP_SECRET));
            //string strSign = DigestUtils.ByteArrayToHexString(HMACSHA256.Compute(strSignText, byteSecretSigning));

            //string strEndPoint = "/service/get-access-token";
            //var client = new RestClient($"{inputInfo.ReqDomainName}");
            //var request = new RestRequest(strEndPoint, Method.Post);

            string strSign = SignUtil.GetSign(dicInput, strTimeStamp, inputInfo.APP_SECRET);

            dicInput.Add("X-FASC-Sign",strSign );
            //request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            //request.AddHeaders(dicInput);
            ////request.AddParameter("application/x-www-form-urlencoded", $"{strSortDicInput}&X-FASC-Sign={strSign}", ParameterType.RequestBody);
            ////request.AddParameter("application/x-www-form-urlencoded", dicInput, ParameterType.RequestBody);
            //var response = await client.ExecuteAsync(request);
            //return response.Content;



            var client = HttpWebHelp.CreateDefault(inputInfo.ReqDomainName + inputInfo.ReqEndPoint);
            foreach (var p in dicInput)
            {
                client.Headers[p.Key] = p.Value;
            }
            client.Method = "POST";
            client.ContentType = "application/x-www-form-urlencoded";
            var rspResponse = await Task.Run(() => client.GetResponseIgnoreServerError());
            var rspStr = rspResponse.GetResponseString();
            var rspToken = JsonConvert.DeserializeObject<ResponseBodyInfo<RespTokenInfo>>(rspStr);
            //rspToken.RequestId = rspResponse.Headers["X-FDD-Request-Id"
            return  rspToken;
        }
    }
}